package edu.cmu.cs.webapp.ioms.ui.dialogs.product;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.FilteredTree;
import org.eclipse.ui.dialogs.PatternFilter;

import edu.cmu.cs.webapp.ioms.dao.DataAccessException;
import edu.cmu.cs.webapp.ioms.dao.impl.ClientDAOImpl;
import edu.cmu.cs.webapp.ioms.models.Client;
import edu.cmu.cs.webapp.ioms.models.Product;
import edu.cmu.cs.webapp.ioms.test.DummieDataGenerator;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

/**
 * 
 * @author Christian
 * 
 */
public class BrowseCustomersDialog extends TitleAreaDialog implements
		ISelectionChangedListener {

	private PatternFilter filteredTreeFilter;
	private Client[] _customers;
	private Client _selectedCustomer;

	public BrowseCustomersDialog(Shell parentShell) {
		super(parentShell);
		try {
			_customers = new ClientDAOImpl().getAllClient().toArray(
					new Client[0]);
		} catch (DataAccessException e) {
			// TODO
			e.printStackTrace();
		}
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		setTitle("Browse Customer List");
		setMessage("Select a customer from the list");

		Composite container = SWTUtils.createComposite(area, 2, SWT.DEFAULT,
				SWT.DEFAULT);

		SWTUtils.createLabel(container, "Customers:", 2);

		GridData data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 280;
		data.horizontalSpan = 2;
		container.setLayoutData(data);

		this.filteredTreeFilter = new PatternFilter() {
			@Override
			public boolean isElementSelectable(Object element) {
				return element instanceof Product;
			}
		};

		FilteredTree filterTree = new FilteredTree(container, SWT.SINGLE
				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER,
				this.filteredTreeFilter);

		TreeViewer viewer = filterTree.getViewer();
		viewer.setContentProvider(new BrowseClientContentProvider());
		viewer.setLabelProvider(new BrowseClientLabelProvider());
		viewer.addSelectionChangedListener(this);
		viewer.setComparator(new ViewerComparator());

		if (_customers != null && _customers.length > 0) {
			viewer.setInput(_customers);
			viewer.expandAll();
		}

		return container;
	}

	public void selectionChanged(SelectionChangedEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event
				.getSelection();
		Object selectedObj = selection.getFirstElement();
		if (selectedObj instanceof Client) {
			this._selectedCustomer = (Client) selectedObj;
		}
	}

	private class BrowseClientContentProvider implements ITreeContentProvider {

		public void dispose() {

		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

		}

		@SuppressWarnings("unchecked")
		public Object[] getChildren(Object parentElement) {
			if (parentElement instanceof DummieDataGenerator) {
				return ((DummieDataGenerator) parentElement).getCustomers();
			}

			if (parentElement instanceof Client[]) {
				return (Client[]) parentElement;
			}
			return new Object[0];
		}

		public Object getParent(Object element) {
			// TODO Auto-generated method stub
			return null;
		}

		public boolean hasChildren(Object element) {
			return getChildren(element).length > 0;
		}

		public Object[] getElements(Object inputElement) {
			return getChildren(inputElement);
		}

	}

	private class BrowseClientLabelProvider implements ILabelProvider {

		public Image getImage(Object element) {
			if (element instanceof Client) {
				return IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_CLIENT);
			}
			return null;
		}

		public String getText(Object element) {
			if (element instanceof Client) {
				return ((Client) element).getFname() + " "
						+ ((Client) element).getLname() + "#("
						+ ((Client) element).getClient_id() + ")";
			}

			return null;
		}

		public void addListener(ILabelProviderListener listener) {

		}

		public void dispose() {

		}

		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		public void removeListener(ILabelProviderListener listener) {

		}

	}

	/**
	 * 
	 * @return
	 */
	public Client getSelectedModel() {
		return this._selectedCustomer;
	}

}
